cmake_minimum_required(VERSION 2.8.12)

project(Slitcanner)
set(target Slitscanner)

add_definitions(-D_UNICODE)
add_definitions(-DUNICODE)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

find_package(DX11 REQUIRED)
find_package(QDX11 REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(OpenCV REQUIRED)
find_package(VideoInput REQUIRED)
find_package(OpenMP REQUIRED)

#include helper
include(${CMAKE_MODULE_PATH}/helper.cmake)
include(${CMAKE_MODULE_PATH}/common.cmake)
#include fxc custom build step
include(${CMAKE_MODULE_PATH}/fxc.cmake)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG /INCREMENTAL:NO")

option(DX11_WINDOW_APP "Set to OFF to use the console subsystem." ON)

# OpenMP specific linker flags
if(OPENMP_FOUND)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()

include_directories(${target}
	${DX11_INCLUDE_DIRS}
	${QDX11_INCLUDE_DIRS}
	${OpenCV_INCLUDE_DIRS}
	${VideoInput_INCLUDE_DIRS}
)

# needs to be there if one wants supress the console window for GUI Applications on Windows
if(WIN32 AND DX11_WINDOW_APP)
	set(GUI_TYPE WIN32)
else()
	set(GUI_TYPE)
endif(WIN32 AND DX11_WINDOW_APP)

set(sources
	src/main.cpp
	src/Dx11Viewer.h
	src/Dx11Viewer.cpp
	src/ui/SlitscanViewer.ui
	src/Math.h
	src/Math.cpp
	src/SlitscanRenderer.h
	src/SlitscanRenderer.cpp
	src/SlitscanRendererFactory.h
	src/SlitscanRendererFactory.cpp
	src/SlitscanWidget.h
	src/SlitscanWidget.cpp
	src/Vertex.h
	src/Vertex.cpp
	src/RenderState.h
	src/RenderState.cpp
	src/InputLayout.h
	src/InputLayout.cpp
	src/Effect.h
	src/Effect.cpp
	src/EffectRegistry.h
	src/EffectRegistry.cpp
	src/GeometryProducer.h
	src/GeometryProducer.cpp
	src/QDebugStream.h
	src/QDebugStream.cpp
	src/SlitscanEffect.h
	src/SlitscanEffect.cpp
	src/Util.h
	src/Util.cpp
	src/VideoSink.h
	src/VideoSink.cpp
	src/ICaptureCam.h
	src/OpenCVCaptureCam.h
	src/OpenCVCaptureCam.cpp
	src/MediaFoundationCaptureCam.h
	src/MediaFoundationCaptureCam.cpp
	src/ChooseDialog.h
	src/ChooseDialog.cpp
	src/ui/ChooseDialog.ui
)

set(effects
	src/fx/slitscan.fx
)

# moc compiler
qt5_auto_wrap(sources ${sources})

add_executable(${target} ${GUI_TYPE} ${sources} ${effects})
configureDebugPostfix("d")
configureSourceGroups()
compileFX("${effects}")

# Use the Widgets module from Qt 5.
qt5_use_modules(${target} Widgets)

target_link_libraries(${target}
	${DX11_LIBRARIES}
	${QDX11_LIBRARIES}
	${OpenCV_LIBS}
	${VideoInput_LIBRARIES}
)

option(SLIT_SCAN_VIDEO_BACKEND_MEDIA_FOUNDATION "Uses Windows Media Foundation as the video backend, set off to use the OpenCV video backend." ON)
if(SLIT_SCAN_VIDEO_BACKEND_MEDIA_FOUNDATION)
	message(STATUS "Windows Media Foundation is used as the video backend.")
	add_definitions(-DSLIT_SCAN_MEDIA_FOUNDATION_BACKEND)
else()
	message(STATUS "OpenCV is used as the video backend.")
endif()

# install .exe and generated .fxo files
install(TARGETS ${target} DESTINATION bin)
FILE(GLOB files "${CMAKE_BINARY_DIR}/*.fxo" "${CMAKE_SOURCE_DIR}/src/ui/*.png" "${CMAKE_SOURCE_DIR}/src/ui/*.bmp")
INSTALL(FILES ${files} DESTINATION bin)